package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.api.QuanZiApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.pojo.Comment;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.dubbo.vo.PageInfo;
import com.tanhua.server.vo.CommentVo;
import com.tanhua.server.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class QuanZiCommentService {
    @DubboReference(version = "1.0.0")
    private QuanZiApi quanZiApi;
    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;
    @Autowired
    private QuanziMQService quanziMQService;
    public PageResult queryCommentsList(String publishId, Integer page, Integer pageSize) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        Long userId = UserThreadLocal.get();
        PageInfo<Comment> pageInfo = this.quanZiApi.queryCommentList(publishId, page, pageSize);
        List<Comment> records = pageInfo.getRecords();
        if (CollUtil.isEmpty(records)){
            return pageResult;
        }
        List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
        List<UserInfo> userInfoList = this.userInfoApi.queryByUserIdList(userIdList);
        List<CommentVo> result = new ArrayList<>();
        for (Comment record : records) {
            CommentVo commentVo = new CommentVo();
            commentVo.setContent(record.getContent());
            commentVo.setCreateDate(DateUtil.format(new Date(record.getCreated()), "HH:mm"));
            commentVo.setId(record.getId().toHexString());
            //点赞数
            commentVo.setLikeCount(Convert.toInt(this.quanZiApi.queryLikeCount(commentVo.getId())));
            //是否点赞
            commentVo.setHasLiked(this.quanZiApi.queryUserIsLike(userId,commentVo.getId()) ? 1:0);
            //评论数
            //commentVo.setCommentCount(Convert.toInt(this.quanZiApi.queryCommentCount(commentVo.getId())));
            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equals(record.getUserId(),userInfo.getUserId())){
                    commentVo.setAvatar(userInfo.getLogo());
                    commentVo.setNickname(userInfo.getNickName());
                    break;
                }
            }
            result.add(commentVo);
        }
        pageResult.setItems(result);

        return pageResult;

    }

    /**
     * 发表评论
     * @param publishId
     * @param content
     * @return
     */
    public Boolean saveComments(String publishId, String content) {
        return this.quanZiApi.saveComment(UserThreadLocal.get(), publishId, content);
    }



    //todo 李金洋
    /*public Long likeComment(String publishId) {

        Long userId = UserThreadLocal.get();
        Boolean result = this.quanZiApi.likeComment(userId, publishId);
        if (result){
            this.quanziMQService.commentPublishMsg(publishId);
        }
        return this.quanZiApi.queryLikeCount(publishId);
    }*/


}
